home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / QuakeTools / src / libqtools / mip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  6.4 KB  |  197 lines

  1. #define    LIBQTOOLS_CORE
  2. #include "../include/libqtools.h"
  3.  
  4. /*
  5.  * MIP-tools
  6.  */
  7.  
  8. struct palpic *GetMipMap(FILE *file, enum mipmapoffset MipLevel) {
  9.   struct mipmap MipMap;
  10.   int MipMapSize, MipMapOffset;
  11.   struct palpic *Picture = 0;
  12.  
  13.   fread(&MipMap, 1, sizeof(struct mipmap), file);
  14.   if ((Picture = pmalloc(LittleLong(MipMap.width) >> MipLevel, LittleLong(MipMap.height) >> MipLevel, 0, MipMap.name))) {
  15.     MipMapSize = (LittleLong(MipMap.width) >> MipLevel) * (LittleLong(MipMap.height) >> MipLevel);
  16.     MipMapOffset = LittleLong(MipMap.offsets[MipLevel]) - sizeof(struct mipmap);
  17.     fseek(file, MipMapOffset, SEEK_CUR);
  18.     fread(Picture->rawdata, 1, MipMapSize, file);
  19.   }
  20.  
  21.   return Picture;
  22. }
  23.  
  24. struct palpic *ParseMipMap(struct mipmap *MipMap, enum mipmapoffset MipLevel) {
  25.   int MipMapSize, MipMapOffset;
  26.   struct palpic *Picture = 0;
  27.  
  28.   if ((Picture = pmalloc(LittleLong(MipMap->width) >> MipLevel, LittleLong(MipMap->height) >> MipLevel, 0, MipMap->name))) {
  29.     MipMapSize = (LittleLong(MipMap->width) >> MipLevel) * (LittleLong(MipMap->height) >> MipLevel);
  30.     MipMapOffset = LittleLong(MipMap->offsets[MipLevel]) - sizeof(struct mipmap);
  31.     memcpy(Picture->rawdata, ((char *)MipMap) + MipMapOffset, MipMapSize);
  32.   }
  33.  
  34.   return Picture;
  35. }
  36.  
  37. /*
  38.  * returns the offset or -1 for fail 
  39.  */
  40. bool PutMipMap(FILE *file, struct palpic *Picture) {
  41.   struct mipmap MipMap;
  42.   int MipMapSize;
  43.   unsigned char *MipBody;
  44.   bool retval = FALSE;
  45.   
  46.   /*
  47.    * fix!!! OP_UPDATE offsets ??? 
  48.    */
  49.   MipMapSize = Picture->width * Picture->height;
  50.   if ((MipBody = (unsigned char *)tmalloc(MipMapSize))) {
  51.     short int x, y, num, dwidth = 1, dheight = 1, dshift = 1;
  52.     unsigned char *bodySrc, *bodyDst;
  53.     struct rgb *Palette = Picture->palette;
  54.  
  55.     strncpy(MipMap.name, Picture->name, NAMELEN_MIP);
  56.     MipMap.height = LittleLong(Picture->height);
  57.     MipMap.width = LittleLong(Picture->width);
  58.     MipMap.offsets[MIPMAP_0] = LittleLong(sizeof(struct mipmap));
  59.     MipMap.offsets[MIPMAP_1] = LittleLong(LittleLong(MipMap.offsets[MIPMAP_0]) + (MipMapSize / (1 * 1)));
  60.     MipMap.offsets[MIPMAP_2] = LittleLong(LittleLong(MipMap.offsets[MIPMAP_1]) + (MipMapSize / (2 * 2)));
  61.     MipMap.offsets[MIPMAP_3] = LittleLong(LittleLong(MipMap.offsets[MIPMAP_2]) + (MipMapSize / (4 * 4)));
  62.  
  63.     fwrite(&MipMap, 1, sizeof(struct mipmap), file);
  64.     fwrite(bodySrc = Picture->rawdata, 1, MipMapSize, file);
  65.  
  66.     for (num = 0; num < 3; num++) {
  67.       bodyDst = MipBody;
  68.       dwidth <<= 1;
  69.       dheight <<= 1;
  70.       dshift++;
  71.       for (y = 0; y < Picture->height; y += dheight) {
  72.     for (x = 0; x < Picture->width; x += dwidth) {
  73.       short int dx, dy;
  74.       short int R = 0, G = 0, B = 0;
  75.       struct rgb rawpix;
  76.  
  77.       for (dy = 0; dy < dheight; dy++) {
  78.         for (dx = 0; dx < dheight; dx++) {
  79.           short int palpix = (short int)bodySrc[((y + dy) * Picture->width) + x + dx];
  80.  
  81.           R += (short int)Palette[palpix].r;
  82.           G += (short int)Palette[palpix].g;
  83.           B += (short int)Palette[palpix].b;
  84.         }
  85.       }
  86.       rawpix.r = (unsigned char)(R >> dshift);
  87.       rawpix.g = (unsigned char)(G >> dshift);
  88.       rawpix.b = (unsigned char)(B >> dshift);
  89.       *bodyDst++ = Match(&rawpix, Palette);
  90.     }
  91.       }
  92.       fwrite(MipBody, 1, MipMapSize / (dheight * dwidth), file);
  93.     }
  94.     tfree(MipBody);
  95.     retval = TRUE;
  96.   }
  97.   else
  98.     eprintf("cannot tmalloc %d bytes-body\n", MipMapSize);
  99.  
  100.   return retval;
  101. }
  102.  
  103. bool PutMipMap0(FILE *file, struct palpic *Picture) {
  104.   struct mipmap MipMap;
  105.  
  106.   strncpy(MipMap.name, Picture->name, NAMELEN_MIP);
  107.   MipMap.height = LittleLong(Picture->height);
  108.   MipMap.width = LittleLong(Picture->width);
  109.   MipMap.offsets[MIPMAP_0] = LittleLong(sizeof(struct mipmap));
  110.   MipMap.offsets[MIPMAP_1] = 0;
  111.   MipMap.offsets[MIPMAP_2] = 0;
  112.   MipMap.offsets[MIPMAP_3] = 0;
  113.   fwrite(&MipMap, 1, sizeof(struct mipmap), file);
  114.   fwrite(Picture->rawdata, 1, Picture->width * Picture->height, file);
  115.  
  116.   return TRUE;
  117. }
  118.  
  119. bool PasteMipMap(struct mipmap *MipMap, struct palpic *Picture) {
  120.   int MipMapSize;
  121.   unsigned char *MipBody;
  122.   bool retval = FALSE;
  123.  
  124.   /*
  125.    * fix!!! OP_UPDATE offsets ??? 
  126.    */
  127.   MipMapSize = Picture->width * Picture->height;
  128.   if ((MipBody = (unsigned char *)tmalloc(MipMapSize))) {
  129.     short int x, y, num, dwidth = 1, dheight = 1, dshift = 1, pos;
  130.     unsigned char *bodySrc, *bodyDst;
  131.     struct rgb *Palette = Picture->palette;
  132.  
  133.     strncpy(MipMap->name, Picture->name, NAMELEN_MIP);
  134.     MipMap->height = LittleLong(Picture->height);
  135.     MipMap->width = LittleLong(Picture->width);
  136.     MipMap->offsets[MIPMAP_0] = LittleLong(sizeof(struct mipmap));
  137.     MipMap->offsets[MIPMAP_1] = LittleLong(LittleLong(MipMap->offsets[MIPMAP_0]) + (MipMapSize / (1 * 1)));
  138.     MipMap->offsets[MIPMAP_2] = LittleLong(LittleLong(MipMap->offsets[MIPMAP_1]) + (MipMapSize / (2 * 2)));
  139.     MipMap->offsets[MIPMAP_3] = LittleLong(LittleLong(MipMap->offsets[MIPMAP_2]) + (MipMapSize / (4 * 4)));
  140.     
  141.     pos = sizeof(struct mipmap);
  142.  
  143.     memcpy(((char *)MipMap) + pos, bodySrc = Picture->rawdata, MipMapSize);
  144.     pos += MipMapSize;
  145.  
  146.     for (num = 0; num < 3; num++) {
  147.       bodyDst = MipBody;
  148.       dwidth <<= 1;
  149.       dheight <<= 1;
  150.       dshift++;
  151.       for (y = 0; y < Picture->height; y += dheight) {
  152.     for (x = 0; x < Picture->width; x += dwidth) {
  153.       short int dx, dy;
  154.       short int R = 0, G = 0, B = 0;
  155.       struct rgb rawpix;
  156.  
  157.       for (dy = 0; dy < dheight; dy++) {
  158.         for (dx = 0; dx < dheight; dx++) {
  159.           short int palpix = (short int)bodySrc[((y + dy) * Picture->width) + x + dx];
  160.  
  161.           R += (short int)Palette[palpix].r;
  162.           G += (short int)Palette[palpix].g;
  163.           B += (short int)Palette[palpix].b;
  164.         }
  165.       }
  166.       rawpix.r = (unsigned char)(R >> dshift);
  167.       rawpix.g = (unsigned char)(G >> dshift);
  168.       rawpix.b = (unsigned char)(B >> dshift);
  169.       *bodyDst++ = Match(&rawpix, Palette);
  170.     }
  171.       }
  172.       memcpy(((char *)MipMap) + pos, MipBody, MipMapSize / (dheight * dwidth));
  173.       pos += MipMapSize / (dheight * dwidth);
  174.     }
  175.     tfree(MipBody);
  176.     retval = TRUE;
  177.   }
  178.   else
  179.     eprintf("cannot tmalloc %d bytes-body\n", MipMapSize);
  180.  
  181.   return retval;
  182. }
  183.  
  184. bool PasteMipMap0(struct mipmap *MipMap, struct palpic *Picture) {
  185.   strncpy(MipMap->name, Picture->name, NAMELEN_MIP);
  186.   MipMap->height = LittleLong(Picture->height);
  187.   MipMap->width = LittleLong(Picture->width);
  188.   MipMap->offsets[MIPMAP_0] = LittleLong(sizeof(struct mipmap));
  189.   MipMap->offsets[MIPMAP_1] = 0;
  190.   MipMap->offsets[MIPMAP_2] = 0;
  191.   MipMap->offsets[MIPMAP_3] = 0;
  192.   memcpy(((char *)MipMap) + sizeof(struct mipmap), Picture->rawdata, Picture->width * Picture->height);
  193.  
  194.   return TRUE;
  195. }
  196.  
  197.